package 学习计划.数据结构;

import java.util.Arrays;

/**
 * @author 会玩的洋洋
 * https://leetcode.cn/problems/single-number/?envType=study-plan&id=shu-ju-jie-gou-ji-chu&plan=data-structures&plan_progress=cqjfoh6
 */
public class _136_只出现一次的数字 {

    /**
        执行用时：6 ms, 内存消耗：42 MB
     */
    public int singleNumber(int[] nums) {
        Arrays.sort(nums);
        int res = nums[0];
        int n = nums.length;
        for (int i = 0; i < n; i += 2) {
            if (nums[i] != nums[i + 1]) {
                if (nums[i + 1] == nums[i + 2]) {
                    res = nums[i];
                } else {
                    res = nums[i + 1];
                }
                break;
            }
        }
        return res;
    }

    /**
     * 执行用时：1 ms, 内存消耗：41.7 MB
     * @param nums
     * @return
     */
    public int singleNumber2(int[] nums) {
        int res = nums[0];
        for (int num : nums) {
            res ^= num;
        }
        return res;
    }

}

